package com.example.kt_all.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.kt_all.R
import com.example.kt_all.bean.resp.OpListResp

/**
 * Created by 小陈 on 2025/4/3.
 * Time: 22:53.
 */
class OperationListRVAdapter(var listener: OperationListItemClickListener) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>(), View.OnClickListener {

    private var diff: AsyncListDiffer<OpListResp.OperationVo>
    private val NORMAL: Int = 0
    private val FOOT: Int = 1
    private val LAST: Int = 2
    var isLastPage = false

    init {
        diff = AsyncListDiffer(this, MyCallback())
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            NORMAL -> {
                MyViewHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.layout_item_operation_list, parent, false)
                )
            }

            FOOT -> {
                MyFootHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(com.example.kt_common.R.layout.foot_rv, parent, false)
                )
            }

            else -> {
                MyLastHolder(
                    LayoutInflater.from(parent.context)
                        .inflate(com.example.kt_common.R.layout.last_rv, parent, false)
                )
            }
        }
    }

    override fun getItemCount(): Int {
        return if (diff.currentList.size == 0) 1 else diff.currentList.size + 1

    }

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) {
            if (isLastPage) {
                LAST
            } else {
                FOOT
            }
        } else {
            NORMAL
        }
    }

    fun setData(list: List<OpListResp.OperationVo>?) {
        //AsyncListDiffer需要一个新数据，不然添加无效
        diff.submitList(if (list != null) ArrayList(list) else null)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        Log.d("HomeRVAdapter", "onBindViewHolder: position = $position")
        if (getItemViewType(position) == NORMAL) {
            val data = diff.currentList[position]
            (holder as MyViewHolder).tv_boardingTime.text = data.onCarTime?:""
            holder.tv_departureTime.setText(data.offCarTime?:"");
            holder.tv_boardingAddress.setText(data.ontoEmptyAddress);
            holder.tv_dropOffAddress.setText(data.emptytoOnAddress);
            holder.tv_pricingMileage.setText(data.mileage);
            holder.tv_deadheadMileage.setText(data.emptyMileage);
            holder.tv_currentTrainNumber.setText(data.currentCarNum);
            holder.tv_paidInAmount.setText(data.realityAmount + " 元");

        }
    }

    class MyViewHolder(item: View) : RecyclerView.ViewHolder(item) {
        var tv_boardingTime: TextView = item.findViewById(R.id.tv_boardingTime);
        var tv_departureTime: TextView = item.findViewById(R.id.tv_departureTime);
        var tv_boardingAddress: TextView = item.findViewById(R.id.tv_boardingAddress);
        var tv_dropOffAddress: TextView = item.findViewById(R.id.tv_dropOffAddress);
        var tv_pricingMileage: TextView = item.findViewById(R.id.tv_pricingMileage);
        var tv_deadheadMileage: TextView = item.findViewById(R.id.tv_deadheadMileage);
        var tv_currentTrainNumber: TextView = item.findViewById(R.id.tv_currentTrainNumber);
        var tv_paidInAmount: TextView = item.findViewById(R.id.tv_paidInAmount);
    }

    class MyFootHolder(item: View) : RecyclerView.ViewHolder(item)

    class MyLastHolder(item: View) : RecyclerView.ViewHolder(item)

    class MyCallback : DiffUtil.ItemCallback<OpListResp.OperationVo>() {
        override fun areItemsTheSame(
            oldItem: OpListResp.OperationVo, newItem: OpListResp.OperationVo
        ): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(
            oldItem: OpListResp.OperationVo, newItem: OpListResp.OperationVo
        ): Boolean {
            return oldItem.carNum == newItem.carNum
        }
    }

    override fun onClick(p0: View?) {
    }
}